package com.jianbing.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jianbing.entity.Result;
import com.jianbing.service.ZXService;
import com.jianbing.util.StringUtils;

import net.sf.json.JSONObject;

/**
 * @author 作者: hzz
 * @version 创建时间：2017年5月18日 下午3:53:21 类说明
 */

@Controller
@RequestMapping("/zx")
public class ZXController {

	Logger logger = Logger.getLogger(ZXController.class);

	@Autowired
	private ZXService zxService;

	/**
	 * 
	 * 合作方获取征信数据
	 * 
	 * @param request
	 * @param response
	 * @param body
	 * @return
	 */
	@RequestMapping(value = "queryZxData", method = RequestMethod.POST)
	@ResponseBody
	public Result queryZxData(HttpServletRequest request, HttpServletResponse response, @RequestBody String body) {
		Result result = new Result();
		logger.info("查询征信数据" + body);
		try {
			JSONObject json = JSONObject.fromObject(body);
			if (json.get("sign") == null || json.get("params") == null) {
				result.setCode(101);
				result.setMsg("参数错误");
				return result;
			}
			return zxService.getZxData(result, json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
	}

	/**
	 * 
	 * 查询征信任务状态
	 * 
	 * @author cwl
	 * @date 2017年6月14日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/queryZxTaskStatus", method = RequestMethod.GET)
	@ResponseBody
	public Result queryZxTaskStatus(HttpServletRequest request, HttpServletResponse response, String orderSn,
			Integer taskId, String status) {
		Result result = new Result();
		ModelMap model = new ModelMap();
		model.addAttribute("list", zxService.queryZxTaskStatus(orderSn, taskId, status));
		result.setData(model);
		return result;
	}

	/**
	 * 
	 * 更新征信任务状态
	 * 
	 * @author cwl
	 * @date 2017年6月14日
	 *
	 * @param request
	 * @param response
	 * @param orderSn
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/updateZxTaskStatus", method = RequestMethod.POST)
	@ResponseBody
	public Result updateZxTaskStatus(HttpServletRequest request, HttpServletResponse response, String orderSn,
			Integer taskId, String status) {
		Result result = new Result();
		if (StringUtils.isEmpty(orderSn) || StringUtils.isEmpty(status) || taskId == null) {
			result.setCode(-1);
			result.setMsg("参数错误");
			return result;
		}
		try {
			zxService.updateZxTaskStatus(orderSn, taskId, status);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setCode(-1);
			result.setMsg("系统异常");
			return result;
		}
		return result;
	}
}
